package com.yingxin.yx.framework.user.dto;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.*;

/**
 * @author fanchao
 */
@Data
@NoArgsConstructor
public class UserDto implements Serializable {

    private static final long serialVersionUID = -8358017011093009055L;

    /**
     * 用户id
     */
    private String userId;


    private String userNo;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * app密码
     */
    private String appPassword;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 实名
     */
    private String staffName;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 电话
     */
    private String phone;

    /**
     * 手机
     */
    private String mobilePhone;

    /**
     * 固定电话
     */
    private String fixedPhone;

    /**
     * qq
     */
    private String qq;

    /**
     * 微信
     */
    private String wechatNickname;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 备注
     */
    private String remarks;

    /**
     * 部门id
     */
    private String deptId;

    /**
     * 用户的角色信息存储roleCode
     */
    protected Set<String> roleSet = new HashSet<>();

    /**
     * 用户的资源权限数据(连接类型)存入资源值
     */
    protected Set<String> resourceSet = new HashSet<>();


    /**
     * 用户的资源权限存入资源值
     */
    protected List<PermissionDto> permissions = new ArrayList<>();

    /**
     * 用户的资源权限存入资源值
     */
    protected List<AppPermissionDto> appPermissions = new ArrayList<>();

    /**
     * rememberMe
     */
    private boolean rememberMe;

    /**
     * 私钥
     */
    private String jwtPrivateKey;

    /**
     * 是否能APP登录
     */
    private String isAppLogon;
    /**
     * APP请求类型
     */
    private String isApp;

    /**
     * 登录Ip
     */
    private String loginIP;
    /**
     * 登录时间
     */
    private Date loginTime;

    public void addResourceSet(List<PermissionDto> permissions) {
        this.permissions = permissions;
        Set<String> roleStringSet = new HashSet<>();
        Set<String> permissionStringSet = new HashSet<>();
        for (PermissionDto permission : permissions) {
            if (StringUtils.isNotEmpty(permission.getPermissionString())) {
                permissionStringSet.add(permission.getPermissionString());
                if (permission.getPermissionString().startsWith("ROLE_")) {
                    roleStringSet.add(permission.getPermissionString());
                }
            }
        }
        this.roleSet = roleStringSet;
        this.resourceSet = permissionStringSet;
    }

}